********************************************************************************
*DYNAMIC IMPACTS OF PRICING GROUNDWATER
*Bruno, Jessoe, Hanemann in JAERE
*This file uses data output from "Parcel_clean.do"
********************************************************************************

clear all
capture log close
set more off

*SELECT OUTPUT DATE
global outputdate = "20231129"		 

*SET DIRECTORY
cd  "D:\Ellen\Dropbox\Pajaro_AgInnovation" 

*Generating different logs based on which data + portions of the code were run
log using "Submission\JAERE\Replication_Code\Log\DinD_quarterly_log.log", replace
*log using "Submission\JAERE\Replication_Code\Log\DinD_quarterly_unbalanced_log.log", replace
*log using "Submission\JAERE\Replication_Code\Log\DinD_annual_log.log", replace

*SELECT DATASET
global dataset 1	 
global quarterly "Data\Parcel_Clean_20230626.dta"
global yearrun "Data\Parcel_Clean_yearrun_20230626.dta"

if $dataset == 1 use $quarterly, clear
if $dataset == 2 use $yearrun, clear

*GENERATE LAG DELIVERIES -- TOGGLE ON/OFF BASED ON CALLED DATASET
sort parcelnum quarter 
by parcelnum: gen lagdelivered = delivered[_n-1]

*sort parcelnum year_run 
*by parcelnum: gen lagdelivered = delivered[_n-1]

********************************************************************************
*TABLE 2: ANNUAL EXTRACTION DIFF IN DIFF
********************************************************************************
*Balance the panel
preserve
bysort parcelnum: gen ntime = [_N]
sum ntime
keep if ntime==`r(max)'

*Drop 2010
drop if year_run==5 
drop if year_run >10


drop if gridid_500m == . 

putexcel set "Tables\price_change_$outputdate.xlsx", replace
bys inside: sum price if post==0
putexcel A1 = "Pre Price"
putexcel B1 = `r(mean)'

sum price if post==1 & inside==1
putexcel A2 = "Post Inside"
putexcel B2 = `r(mean)'
sum price if post==1 & inside==0
putexcel A3 = "Post Outside"
putexcel B3 = `r(mean)'


bys inside: sum extraction if post==0

eststo clear

*Column 1: No FE
eststo: reg extraction treatment post inside if year_run <11, cluster(parcelnum)
	estadd local wellFE " "
	estadd local timeFE " "
	estadd local controls " "
	predict water_hat_1
	gen water_hat_nettreat_1 = water_hat_1-_b[treatment]
	gen change_1 = _b[treatment]/water_hat_nettreat_1
*Column 2: Add well FE
eststo: xtreg extraction treatment post if year_run <11, fe cluster(parcelnum) 
	estadd local wellFE " \checkmark  "
	estadd local timeFE " "
	estadd local controls " "
	predict water_hat_2
	gen water_hat_nettreat_2 = water_hat_2-_b[treatment]
	gen change_2 = _b[treatment]/water_hat_nettreat_2
*Column 3: Add year FE
eststo: xtreg extraction treatment i.year_run if year_run <11, fe cluster(parcelnum)
	estadd local wellFE " \checkmark  "
	estadd local timeFE " \checkmark  "
	estadd local controls " "
	predict water_hat_3
	gen water_hat_nettreat_3 = water_hat_3-_b[treatment]
	gen change_3 = _b[treatment]/water_hat_nettreat_3
*Column 4: Add county-year FE
eststo: xtreg extraction treatment i.year_run#i.county if year_run <11, fe cluster(parcelnum)
	estadd local wellFE " \checkmark  "
	estadd local timeFE " "
	estadd local controls " \checkmark  "
	predict water_hat_4
	gen water_hat_nettreat_4 = water_hat_4-_b[treatment]
	gen change_4 = _b[treatment]/water_hat_nettreat_4
esttab using "Tables\Extraction_DinD_$outputdate.tex", label replace ///
	se star(* .10 ** .05 *** .01) ///
	addnote(Table reports results from diff-in-diff regression. Standard errors are clustered at the parcel level.) ///
	keep(_cons post treatment inside) ///
	scalars("wellFE Parcel FE" "timeFE Year FE" "controls County-Year FE") ///
	cells(b(star fmt(2)) se(par fmt(2)))
eststo clear

********************************************************************************
*TABLE 10: VARY CLUSTERING OF STANDARD ERRORS
********************************************************************************
*Column 1: main - parcelnum 
eststo: xtreg extraction treatment i.year_run#i.county if year_run <11, fe cluster(parcelnum)
	estadd local wellFE " \checkmark  "
	estadd local controls " \checkmark "
	estadd local clusters " 624 "
*Column 2: 400m
eststo: xtreg extraction treatment i.year_run#i.county if year_run <11, fe cluster(gridid_400m)
	estadd local wellFE " \checkmark  "
	estadd local controls " \checkmark "
	estadd local clusters " 531 "
*Column 3: 500m
eststo: xtreg extraction treatment i.year_run#i.county if year_run <11, fe cluster(gridid_500m)
	estadd local wellFE " \checkmark  "
	estadd local controls " \checkmark "
	estadd local clusters " 456 "
*Column 4: 600m
eststo: xtreg extraction treatment i.year_run#i.county if year_run <11, fe cluster(gridid_600m)
	estadd local wellFE " \checkmark  "
	estadd local controls " \checkmark "
	estadd local clusters " 405 "
*Column 5: 1km
eststo: xtreg extraction treatment i.year_run#i.county if year_run <11, fe cluster(gridid_1000m)
	estadd local wellFE " \checkmark  "
	estadd local controls " \checkmark "
	estadd local clusters " 222 "
esttab using "Tables\Extraction_DinD_cluster_combo_$outputdate.tex", label replace ///
	se star(* .10 ** .05 *** .01) ///
	addnote(Table reports results from an annual difference-in-differences regression at the parcel level. Standard errors are clustered at various levels of spatial aggregation. *, **, *** denote significance at the 10\%, 5\%, and 1\% levels. A grid with 500m-length squares contains 4 average-sized parcels (or 9 median-sized parcels).) ///
	keep(_cons treatment) ///
	scalars("wellFE Parcel FE" "controls County-Year FE") ///
	cells(b(star fmt(2)) se(par fmt(2)))
eststo clear
restore

********************************************************************************
**TABLE 3: Extraction pre-trends table test
********************************************************************************
*HAVE TO BE SEPARATE BECAUSE THEY CALL SEPARARE DATASETS DEFINED AT TOP
*USE YEAR_RUN DATA HERE
*COLUMNS (1) and (2) 
eststo clear
preserve
keep if post==0
*Step 1: year FE
eststo: reg extraction year_run i.inside#c.year_run inside i.year_run
estadd local year "\checkmark"
estadd local yc " "	
*Step 1: county-year FE
eststo: reg extraction year_run i.inside#c.year_run inside i.year_run#i.county
estadd local year  " "
estadd local yc "\checkmark"	
restore
esttab using "Tables\Ptrends_table_year_$outputdate.tex", label replace ///
	se star(* .10 ** .05 *** .01) scalars( "year Year FE" "yc Year-County FE") ///
	cells(b(star fmt(2)) se(par fmt(2))) 

*USE QUARTERLY DATA HERE
*COLUMNS (3) and (4) 	
eststo clear
preserve
drop if year<2005
drop if year==2005 | year ==2010
*keep if quarter==3
keep if post==0
*Step 1: no controls
eststo: reg extraction c.time i.inside#c.time inside i.quarter#i.year 
estadd local time "\checkmark"
estadd local yc " "		
*Step 2: condition on these observables, and find no pre-trend. 
eststo: reg extraction c.time i.inside#c.time inside i.quarter#i.year i.year#i.county
estadd local time  "\checkmark "
estadd local yc "\checkmark"	
restore

esttab using "Tables\Ptrends_table_quarter_$outputdate.tex", label replace ///
	se star(* .10 ** .05 *** .01) scalars( "time Time FE" "yc Year-County FE") ///
	cells(b(star fmt(2)) se(par fmt(2)))
eststo clear



********************************************************************************
*Table 2 and 8: QUARTERLY EXTRACTION DIFF IN DIFF: ROBUSTNESS TEST
********************************************************************************
*Table 8 is unbalanced. Table 2 second half is balanced. 

*ALTER FOR BALANCED OR UNBALANCED ROBUSTNESS TABLE (AND CHANGE HOW TABLE IS SAVED):
*Balance the panel
bysort parcelnum: gen ntime = [_N]
sum ntime
keep if ntime==`r(max)'

*DROP 2010
drop if year==2010

*Make sample set consistent across robustness tests
drop if delivered ==. 
drop if CL_zonedate ==. 
drop if gw_depth ==. 

*Calculate pre-period mean for inside zone to report in table 
bys inside: sum extraction if post==0
preserve
drop if lagdelivered==.
bys inside: sum extraction if post==0 
restore

*Quarterly Robustness table: 
eststo clear
*Column 1: Baseline
eststo: xtreg extraction treatment i.quarter#i.year i.year#i.county if year<2016, fe cluster(parcelnum)
	estadd local wellFE "Y"
	estadd local timeFE "Y"
	estadd local controls "Y"	
*Column 2: Main specification w/ deliveries
eststo: xtreg extraction treatment i.quarter#i.year i.year#i.county delivered if year<2016, fe cluster(parcelnum)
	estadd local wellFE "Y"
	estadd local timeFE "Y"
	estadd local controls "Y"	
*Column 3: plus lagged deliveries
eststo: xtreg extraction treatment i.quarter#i.year i.year#i.county delivered lagdelivered if year<2016, fe cluster(parcelnum)
	estadd local wellFE "Y"
	estadd local timeFE "Y"
	estadd local controls "Y"	
*Column 4: 	Add control for chlorides
eststo: xtreg extraction treatment i.quarter#i.year i.year#i.county CL_zonedate if year<2016, fe cluster(parcelnum)
	estadd local wellFE "Y"
	estadd local timeFE "Y"
	estadd local controls "Y"	
*Column 5: Add control for gw depth
eststo: xtreg extraction treatment i.quarter#i.year i.year#i.county gw_depth if year<2016, fe cluster(parcelnum)
	estadd local wellFE "Y"
	estadd local timeFE "Y"
	estadd local controls "Y"	
*Column 6: Add controls for all
eststo: xtreg extraction treatment i.quarter#i.year i.year#i.county delivered CL_zonedate gw_depth if year<2016, fe cluster(parcelnum)
	estadd local wellFE "Y"
	estadd local timeFE "Y"
	estadd local controls "Y"
esttab using "Tables\Parcel_Extract_DinD_robust_balanced_$outputdate.tex", label replace ///
	se star(* .10 ** .05 *** .01) ///
	addnote(Table reports results from diff-in-diff regression. Standard errors are clustered at the parcel level. Chloride measures groundwater salinity.  Time FE are quarter-year interactions.) ///
	keep(_cons treatment) ///
	scalars("wellFE Parcel FE" "timeFE Quarter-Year FE" "controls County-Year FE") ///
	cells(b(star fmt(2)) se(par fmt(2)))
eststo clear

log close

